package com.dt.dubai.core.comm;


import com.dt.dubai.core.utile.CommonUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.*;

import static java.util.Locale.ENGLISH;

/**
 * 常用的工具类
 */
public class Tools {

    public static void andEqualToCriteria(Example.Criteria criteria, Map<String, Object> map, final Long userId) {

        for (Map.Entry<String, Object> mp : map.entrySet()) {
            criteria.andEqualTo(mp.getKey(), mp.getValue());
        }
    }


    // 创建entity 的时间
    public static void createEntityDate(BaseEntity entity) {
        entity.setCreateTxStamp(new Timestamp(System.currentTimeMillis()));
        entity.setLastupTxStamp(new Timestamp(System.currentTimeMillis()));
    }

    // 更新entity 的时间
    public static void updateEntityDate(BaseEntity entity) {
        entity.setLastupTxStamp(new Timestamp(System.currentTimeMillis()));
    }

    public static void convertEntityToMap(Map<String, Object> resultlist, Object entity) {

        if (entity instanceof BaseEntity) {
            try {
                // fields 为空时，返回所有
                Field[] stSuppField = entity.getClass().getSuperclass().getDeclaredFields();
                Field[] stField = entity.getClass().getDeclaredFields();
                stField = ArrayUtils.addAll(stField, stSuppField);
                for (Field fieled : stField) {
                    fieled.setAccessible(true);
                    PropertyDescriptor prop = new PropertyDescriptor(fieled.getName(), entity.getClass());
                    Object value = prop.getReadMethod().invoke(entity); // 读取属性值
                    resultlist.put(fieled.getName(), value);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static void convertListEntityToMap(List<Map<String, Object>> resultlist, List<? extends BaseEntity> entitylist) {

        for (BaseEntity entity : entitylist) {
            Map<String, Object> map = new HashMap<>();
            try {
                Field[] stSuppField = entity.getClass().getSuperclass().getDeclaredFields();
                Field[] stField = entity.getClass().getDeclaredFields();
                stField = ArrayUtils.addAll(stField, stSuppField);
                for (Field fieled : stField) {
                    fieled.setAccessible(true);
                    PropertyDescriptor prop = new PropertyDescriptor(fieled.getName(), entity.getClass());
                    Object value = prop.getReadMethod().invoke(entity);
                    map.put(fieled.getName(), value);
                }

            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
            resultlist.add(map);
        }
    }


    public static void buildJoinConditiion(List<PageCondition> joinCondition, Example.Criteria criteria) {

        if (null == joinCondition) return;
        for (PageCondition condition : joinCondition) {
            if (StringUtils.isEmpty(condition.getValue())) continue;
            // eq相等  ne、neq不相等，  gt大于， lt小于 gte、ge大于等于  lte、le 小于等于  not非  mod求模
            if (condition.getOperate().equals(ConstantValue.String_eq)) {
                criteria.andEqualTo(condition.getKey(), condition.getValue());
            } else if (condition.getOperate().equals(ConstantValue.String_lk)) { // like
                criteria.andLike(condition.getKey(), ConstantValue.String_lk_s + condition.getValue() + ConstantValue.String_lk_s);
            } else if (condition.getOperate().equals(ConstantValue.String_ba)) { // >= 和 <= 之间的值
                String value = condition.getValue();
                if (null == value || value.length() == ConstantValue.INT_1 || !value.contains(ConstantValue.SPLIT__)) continue;
                String dates[] = value.split(ConstantValue.SPLIT__);  // _时间
                if (CommonUtil.isDate(dates[ConstantValue.INT_0]) && CommonUtil.isDate(dates[ConstantValue.INT_1])) {
                    criteria.andGreaterThanOrEqualTo(condition.getKey(), dates[ConstantValue.INT_0]);
                    criteria.andLessThanOrEqualTo(condition.getKey(), dates[ConstantValue.INT_1]);
                } else if (CommonUtil.isDate(dates[ConstantValue.INT_0])) {// 大于等于 开始日期
                    criteria.andGreaterThanOrEqualTo(condition.getKey(), dates[ConstantValue.INT_0]);
                } else if (CommonUtil.isDate(dates[ConstantValue.INT_1])) { // 小于等于 结束日期
                    criteria.andLessThanOrEqualTo(condition.getKey(), dates[ConstantValue.INT_1]);
                }
            } else if (condition.getOperate().equals(ConstantValue.String_nlk)) { // not like
                criteria.andNotLike(condition.getKey(), condition.getValue() + ConstantValue.String_lk_s);
            } else if (condition.getOperate().equals(ConstantValue.String_gt)) { // 大于
                criteria.andGreaterThan(condition.getKey(), condition.getValue());
            } else if (condition.getOperate().equals(ConstantValue.String_lt)) { // 小于
                criteria.andLessThan(condition.getKey(), condition.getValue());
            } else if (ConstantValue.Str_date_start.equals(condition.getKey())) {
                criteria.andGreaterThanOrEqualTo(condition.getOperate(), condition.getValue());
            } else if (ConstantValue.Str_date_end.equals(condition.getKey())) {
                criteria.andLessThanOrEqualTo(condition.getOperate(), condition.getValue());
            }
        }
    }


}
